package com.gooddare.view.component 
{
	import com.gooddare.locator.AssetsLocator;
	import com.gooddare.model.ModelLocator;
	import com.gooddare.model.vo.GridviewVO;
	import com.greensock.easing.Bounce;
	import com.riatimes.mvc.view.BaseComponent;
	import com.riatimes.util.Reflection;
	import flash.display.Sprite;
	import com.greensock.easing.Quad;
	import com.greensock.TweenMax;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Lyleff
	 */
	public class GridViewComponent extends BaseComponent
	{
		private static const pad:int = 5;
		private var gridView:Sprite;
		private var vo:GridviewVO;
		private var gvArr:Array;
		private var len:int;
		private var upArrows:Sprite;
		private var downArrows:Sprite;
		private var headY:Number;
		private var hei:Number = 70;//gridview的高度
		private var wid:Number = 406.7;//gridview的宽度
		private var catchNum:int = 8;//缓存显示个数 8个
		private var catched:int = 3;
		private var head:int = 0;
		private var tail:int = 0;
		private var upB:Boolean = false;
		private var downB:Boolean = false;
		public function GridViewComponent(vo:GridviewVO) 
		{
			this.vo = vo;
			gvArr = new Array();
		}
		override public function initUI():void {
			createArrows();//创建箭头
			
			catchNum = vo.gridlist.length > catchNum?catchNum:vo.gridlist.length;
			for (var i:uint = 0; i < catchNum; i++ ) {
				createGV(i);
			}
			len = gvArr.length > 5?5:gvArr.length;
			headY = (620 - hei * len - pad * (len-1)-hei/2 ) / 2+15;
			
			for (var j:uint = 0; j < len; j++ ) {
				this.addChild(gvArr[j]);
				TweenMax.to(gvArr[j], 0.5, { delay:j * 0.15, y:headY +(hei + 10) * j, alpha:1, ease:Quad.easeOut } );
				tail++;
			}
		}

		/**
		 * 创建箭头
		 */
		private function createArrows():void 
		{
			upArrows = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_ARROWS_UI) as Sprite;
			downArrows = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_ARROWS_UI) as Sprite;
			this.addChild(upArrows);
			upArrows.rotation = 180;
			upArrows.x = 995 / 2 - 15;
			upArrows.y = 65+15;
			this.addChild(downArrows);
			downArrows.x = 995 / 2 - 15;
			downArrows.y = 540+15;
			upArrows.alpha = downArrows.alpha = 0;
			upArrows.buttonMode = downArrows.buttonMode = true;
			TweenMax.to(upArrows, 0.8, { alpha:0.7 } );
			TweenMax.to(downArrows, 0.8, { alpha:0.7 } );
			
			upArrows.addEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
			downArrows.addEventListener(MouseEvent.CLICK, __mouseClickEventHandle);
			
			upArrows.addEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
			downArrows.addEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
			clickByUp(false);
		}
		
		/**
		 * 按钮无效后变灰色
		 * 效果
		 * @param	b
		 */
		public function clickByUp(b:Boolean):void {
			if (b) {
				upArrows.addEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
				TweenMax.to(upArrows, .5, { tint:0x000000 } );
				upArrows.buttonMode = true;
				upB = false;
			}else {
				upArrows.removeEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
				TweenMax.to(upArrows, .5, { tint:0x999999 } );
				upArrows.buttonMode = false;
				upB = true;
			}
		}
		public function clickByDown(b:Boolean):void {
			if (b) {
				downArrows.addEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
				TweenMax.to(downArrows, .5, { tint:0x000000 } );
				downArrows.buttonMode = true;
				downB = false;
			}else {
				downArrows.removeEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
				TweenMax.to(downArrows, .5, { tint:0x999999 } );
				downArrows.buttonMode = false;
				downB = true;
			}
		}
		
		/**
		 * GridView上下翻滚 
		 * 效果
		 * @param	e
		 */
		private function __mouseClickEventHandle(e:MouseEvent):void 
		{
			switch(e.currentTarget) {
				case upArrows:
					if (head <= 0) {
						return;
					}
					if (head == 1) {
						clickByUp(false);
					}
					head--;//首--
					tail--;
					TweenMax.to(gvArr[tail], 0.5, { alpha:0, y:850,ease:Quad.easeIn } );
					for (var i:uint = head,j:uint=0; i < tail; i++,j++ ) {//i控制图片的角标，j控制位置
						TweenMax.to(gvArr[i], 0.5, { alpha:1,y:headY +(hei + 10) * j } );
					}
					if (downB) {
						clickByDown(true);
					}
				break;
				case downArrows:
					if (tail >= vo.gridlist.length) {
						return;
					}
					if (tail == vo.gridlist.length-1) {
						clickByDown(false);
					}
					head++;//首++
					tail++;//尾巴++
					TweenMax.to(gvArr[head - 1], 0.5, { alpha:0, y: -250,ease:Quad.easeIn } );
					this.addChild(gvArr[tail - 1]);
					for (var k:uint = head,l:uint=0; k < tail; k++,l++ ) {//i控制图片的角标，j控制位置
						TweenMax.to(gvArr[k], 0.5, { alpha:1,y:headY +(hei + 10) * l } );
					}
					if (vo.gridlist.length > gvArr.length&&tail+catched>gvArr.length) {
						createGV(gvArr.length);
					}
					if (upB) {
						clickByUp(true);
					}
				break;
			}
		}
		
		/**
		 * 创建GridView
		 */
		private function createGV(id:int):void {
			var gvic:GridViewItemComponent = new GridViewItemComponent(id,vo.gridlist[id]);
			//this.addChild(gvic);
			gvic.alpha = 0;
			gvic.x = 995 / 2 - wid / 2 - 15;
			gvic.y = 800;
			gvArr.push(gvic);
		}
		
		override public function initLocation():void {
		}
		override public function initEvent (b:Boolean = true) : void {
			if (b) {
			}else {
			}
		}
		
		/**
		 * 鼠标移过，跳动一下
		 * 效果
		 * @param	e
		 */
		private function __mouseRollEventHandle(e:MouseEvent):void 
		{
			upArrows.removeEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
			downArrows.removeEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
			switch(e.currentTarget) {
				case upArrows:
					TweenMax.to(upArrows, .3, { y:60+15,onComplete:easeUpBack } );
				break;
				case downArrows:
					TweenMax.to(downArrows, .3, { y:545+15, onComplete:easeDownBack } );
				break;
			}
		}
		/**
		 * 跳动折回
		 * 效果
		 */
		public function easeDownBack():void {
			TweenMax.to(downArrows, .3, { y:535+15,onComplete:addRollEvent } );
		}
		public function easeUpBack():void {
			TweenMax.to(upArrows, .3, { y:70+15,onComplete:addRollEvent } );
		}
		
		/**
		 * 折回后，添加侦听
		 */
		public function addRollEvent():void {
			if (!upB) {
				upArrows.addEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
			}
			if (!downB) {
				downArrows.addEventListener(MouseEvent.ROLL_OVER, __mouseRollEventHandle);
			}
		}
		
		public function ClearItem():void {
			TweenMax.to(upArrows, 0.5, { alpha:0} );
			TweenMax.to(downArrows, 0.5, { alpha:0} );
			var top:int = tail;
			for (var i:int = 0; i < 5; i++ ) {
				top--;
				TweenMax.to(gvArr[top], 0.5, {delay:0.1*i, y:800 } );
			}
		}
	}
}